#===============================================
# Code written by Joe France (joeprogram.com)
# Released under code.google.com/p/video-scholar/
# with the MIT license (aka, the do-whatever-you-want-with-it license)
#===============================================


from lxml import etree
import pyglet
from pyglet.window import key

from CompoundGUI import CompoundGUI
from GUI import GUI
from MarkGroup import MarkGroup

class Timeline( CompoundGUI ):
	'''
	Timeline renders a timeline on the screen
	That follows the video
	And can be marked upon.
	'''
	
	X_BAR = 25
	Y_BAR = 25
	
	def __init__(self, state):
				
		super(Timeline, self).__init__()				
				
		self.dragging = False
		
		self.state = state
		self.video_player = None # The video player associated with this timeline
		self.duration = 0 # The length of the video this timeline represents
		self.set_coordinates( self.X_BAR, self.Y_BAR )
		self.bar = GUI( 'timeline.png' )
		self.bar.set_coordinates( self.X_BAR, self.Y_BAR )
		self.handle = GUI( 'handle.png', origin = 'center center' )
		self.handle.set_coordinates( self.X_BAR, self.Y_BAR + self.bar.get_height()/2 )
		
		self.guis = [ self.bar, self.handle ]
		self.mark_groups = []
		
	def set_context(self, video_player ):
		'''
		Prepares this timeline to represent a specific video
		'''
		self.video_player = video_player
		self.duration = video_player.get_duration()		
		self.seconds_per_pixel = self.duration / self.bar.get_width()
		
	def register_hot_key( self, hot_key ):
		'''
		Informs the Timeline that a new hotkey has been added.
		Allows it to perform the necessary setup to support it.
		'''
		
		mark_group = MarkGroup( timeline = self, hot_key = hot_key )
		self.mark_groups.append( mark_group )
			
		
	def draw( self ):
		
		if not self.dragging:
			position = int(self.video_player.get_time() / self.seconds_per_pixel) + self.X_BAR
			self.move_handle( position )
		
		self.bar.draw()
		for group in self.mark_groups:
			group.draw()		
		self.handle.draw()
				
	def mouse_press( self, x, y, button, modifiers ):
		'''
		Clicks on the timeline or handle allow the user to seek the video
		'''
		self.dragging = False
		self.move_handle( x )
		self.video_player.seek( self.handle_position_to_seconds() )

	def move_handle( self, x ):
		'''
		Sets the coordinates for the handle
		Capping the position at the ends of the bar
		'''
		self.handle.set_coordinates( x = min(max(x,self.X_BAR ),self.X_BAR + self.bar.get_width()) )
		
	def handle_position_to_seconds( self ):
		'''
		Calculates the time stamp the video 
		should seek to based on the position of the handle
		'''
		return (self.handle.get_coordinates()[0] - self.X_BAR) * self.seconds_per_pixel		
			
	def import_xml( self, node ):
		'''
		Sets up the data in the timeline from an xml file
		
		Timeline assumes that the hotkeys are already setup
		So that it can create the Mark Groups
		'''
		
		for hot_key_node in node.findall( 'hot_key' ):
			hot_key = self.state.get_hot_key( hot_key_node.get('key') )
			mark_group = MarkGroup( timeline = self, hot_key = hot_key )
			mark_group.import_xml( hot_key_node )
			self.mark_groups.append( mark_group )
			
	def export_xml( self, node ):
		'''
		Appends a representation of this in xml to the parent node
		'''
		
		for mark_group in self.mark_groups:
			mark_group.export_xml( node )		